package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 根据用户ID获取用户（主播）基本信息
     *
     * @param userId
     * @return
     */
    @Operation(summary = "根据用户ID获取用户（主播）基本信息")
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVoById(@PathVariable Long userId) {
        UserInfoVo userInfoVo = userInfoService.getUserInfoVoById(userId);
        return Result.ok(userInfoVo);
    }


    /**
     * 专辑下声音某一页中需要验证专辑或声音购买状态
     *
     * @param userId               用户ID
     * @param albumId              专辑ID
     * @param needCheckTrackIdList 需要检查声音购买状态ID集合
     * @return Map<声音ID ， 购买状态>
     */
    @Operation(summary = "专辑下声音某一页中需要验证专辑或声音购买状态")
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable Long userId, @PathVariable Long albumId, @RequestBody List<Long> needCheckTrackIdList) {
        Map<Long, Integer> buyStatusMap = userInfoService.userIsPaidTrack(userId, albumId, needCheckTrackIdList);
        return Result.ok(buyStatusMap);
    }


    /**
     * 判断当前用户是否购买过指定专辑
     *
     * @param albumId 专辑ID
     * @return true:已购买 false：未购买
     */
    @GuiGuLogin
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        Boolean isBuy = userInfoService.isPaidAlbum(userId, albumId);
        return Result.ok(isBuy);
    }


    /**
     * 查询当前登录用户某个专辑已购声音ID列表
     *
     * @param albumId
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "查询当前登录用户某个专辑已购声音ID列表")
    @GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> getUserPaidTrackIdList(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        List<Long> userPaidTrackIdList = userInfoService.getUserPaidTrackIdList(userId, albumId);
        return Result.ok(userPaidTrackIdList);
    }


    /**
     * 新增用户购买记录:VIP会员、专辑、声音
     *
     * @param userPaidRecordVo
     * @return
     */
    @GuiGuLogin(required = false)  //微信回调不会携带Token导致该方法调用失败
    @Operation(summary = "新增用户购买记录")
    @PostMapping("/userInfo/savePaidRecord")
    public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        Long userId = AuthContextHolder.getUserId();
        if(userId!=null){
            userPaidRecordVo.setUserId(userId);
        }
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }


    /**
     * 更新会员过期用户VIP标识
     * @return
     */
    @Operation(summary = "更新Vip到期失效状态")
    @GetMapping("/updateVipExpireStatus")
    public Result updateVipExpireStatus() {
        userInfoService.updateVipExpireStatus();
        return Result.ok();
    }
}

